package com.example.practice.db;

import java.util.Date;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.example.practice.bean.SensorValue;

/**
 * 传感器值的数据库类
 * 
 * @author asus
 *
 */
public class SensorValueDB extends BaseDB {
	// 表名称
	public static final String TABLE_NAME = "SensorValue";
	// 表主键
	private static final String KEY_ROWID = "_id";
	private static final String KEY_CO2 = "co2";
	private static final String KEY_LIGHT = "light";
	private static final String KEY_AIR_T = "airTemper";
	private static final String KEY_AIR_H = "airHumid";
	private static final String KEY_SOIL_T = "soilTemper";
	private static final String KEY_SOIL_H = "soilHumid";
	public static final String KEY_UPDATA_TIME = "updataTime";
	// 创建表的sql语句
	public static final String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + KEY_ROWID
			+ " integer primary key autoincrement, " + KEY_CO2 + " int, "
			+ KEY_LIGHT + " int, " + KEY_AIR_T + " int, " + KEY_AIR_H
			+ " int, " + KEY_SOIL_T + " int, " + KEY_SOIL_H + " int, "
			+ KEY_UPDATA_TIME + " long " + ");";

	public SensorValueDB(Context ctx) {
		super(ctx, true);
	}

	/**
	 * 获取表名称
	 * 
	 * @see lenovo.agriculture.querydata.db.BaseDB#getTableName()
	 */
	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}

	/**
	 * 将Object转化成ContentValues
	 * 
	 * @see lenovo.agriculture.querydata.db.BaseDB#getContentValuesByData(java.lang.Object)
	 */
	@Override
	public ContentValues getContentValuesByData(Object data) {
		SensorValue sensor = (SensorValue) data;
		if (sensor == null) {
			return null;
		}
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_CO2, sensor.getCo2());
		initialValues.put(KEY_LIGHT, sensor.getLight());
		initialValues.put(KEY_AIR_T, sensor.getAirTemper());
		initialValues.put(KEY_AIR_H, sensor.getAirHumid());
		initialValues.put(KEY_SOIL_T, sensor.getSoilTemper());
		initialValues.put(KEY_SOIL_H, sensor.getAirHumid());

		// 更新时间以时间戳的方式保存
		if (sensor.getUpdataTime() == null) {
			sensor.setUpdataTime(new Date());
		}
		initialValues.put(KEY_UPDATA_TIME, sensor.getUpdataTime().getTime());
		return initialValues;
	}

	/**
	 * 获取Object
	 * 
	 * @see lenovo.agriculture.querydata.db.BaseDB#getDataByCursor(android.database.Cursor)
	 */
	@Override
	public Object getDataByCursor(Cursor cursor) {
		//填写以下内容

		if(cursor == null){
			return null;
		}
		SensorValue sensorValue = new SensorValue();
		sensorValue.setAirHumid(cursor.getInt(cursor.getColumnIndex(KEY_AIR_H)));
		sensorValue.setAirTemper(cursor.getInt(cursor.getColumnIndex(KEY_AIR_T)));
		sensorValue.setSoilHumid(cursor.getInt(cursor.getColumnIndex(KEY_SOIL_H)));
		sensorValue.setSoilTemper(cursor.getInt(cursor.getColumnIndex(KEY_SOIL_T)));
		sensorValue.setCo2(cursor.getInt(cursor.getColumnIndex(KEY_CO2)));
		sensorValue.setLight(cursor.getInt(cursor.getColumnIndex(KEY_LIGHT)));
		long time = cursor.getLong(cursor.getColumnIndex(KEY_UPDATA_TIME));
		if(time > 0){
			sensorValue.setUpdataTime(new Date(time));
		}else{
			sensorValue.setUpdataTime(new Date());
		}
		return sensorValue;
	}

	/**
	 * 从数据库中读取传感器的值，更新时间戳必须大于指定值
	 * 
	 * @param timeMills
	 *            时间戳限制条件
	 * @param listener
	 *            读取结果的回调函数
	 */
	public void loadSensorByTime(long timeMills,
			OnDatabaseHandlerListener listener) {
		if (timeMills <= 0) {
			loadDataFromDB(null, listener);
		} else {
			loadDataFromDB(KEY_UPDATA_TIME + ">=" + timeMills, listener);
		}
	}

	/**
	 * 从数据库中删除时间戳小于指定值的传感器数据
	 * 
	 * @param timeMills
	 */
	public void deleteSensorByTime(final long timeMills) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				synchronized (_writeLock) {
					mDBHelper.getWritableDatabase().delete(getTableName(),
							KEY_UPDATA_TIME + "<=" + timeMills, null);
				}
			}
		}.start();
	}
}
